require "Common/define"
require "Logic/Config"
require "Common/functions"

UIDrawChooseHero = {};
HelperFunc.SetUIEnv(UIDrawChooseHero)
local this = UIDrawChooseHero;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.Herolist=nil;
this.choose=nil;
this.btn_ture=nil;
this.backBtn=nil;
this.selecthero=nil;
this.title=nil;
this.ChooseDesc=nil;

this.data=nil;
this.selecthero=nil;
this.selectObj=nil;
this.ctrlHadBattle=nil;

function UIDrawChooseHero.Init(data,selecthero)
    this.data = data;
    this.selecthero =tonumber(selecthero);
    local name, packageName = getUIName(PanelNames.UIDrawChooseHero);
    panelMgr:ShowUI(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIDrawChooseHero.onCreate(obj)

    Herolist=obj:GetChild("n95");
    choose=obj:GetChild("choose");
    btn_ture=obj:GetChild("n103");
    backBtn=obj:GetChild("backBtn")
    state=obj:GetController("state");
    title=obj:GetChild("n93"):GetChild("title");
    ChooseDesc=obj:GetChild("ChooseDesc");

    title.text=getLanguage("Draw_TargetDraw_Title_SelectHero");
    ChooseDesc.text=getLanguage("Draw_TargetDraw_Rule");
    Herolist.itemRenderer=this.HerolistData;
    Herolist.numItems=#this.data;
    this.SetChoose();
    this.addEvent();

end
function UIDrawChooseHero.SetChoose()
    if selecthero then
        state.selectedIndex=1;
        local curHeroData = logicMgr.HeroManager.getHeroDatabyModelId(tostring(selecthero),true);
        local ctrlItem = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
        local data = {};
        data.star = logicMgr.HeroManager.starToShow(logicMgr.HeroManager.GetHeroStarByModelId(tostring(selecthero)));
        data.icon = curHeroData.icon;
        data.quality = logicMgr.HeroManager.GetHeroQualityByModelId(tostring(selecthero));
        data.type = curHeroData.type;
        ctrlItem.initData(choose,data);
        else
            state.selectedIndex=0;
    end
    
end

function UIDrawChooseHero.HerolistData(index,itemObj)
    local itemVal = this.data[index+1];
        local go = itemObj
        local curHeroData = logicMgr.HeroManager.getHeroDatabyModelId(tostring(itemVal),true);
        
        

        local ctrlItem = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
        if ctrlItem ~= nil then
            local data = {};
            data.star = logicMgr.HeroManager.starToShow(logicMgr.HeroManager.GetHeroStarByModelId(tostring(itemVal)));
            data.icon = curHeroData.icon;
            data.quality = logicMgr.HeroManager.GetHeroQualityByModelId(tostring(itemVal));
            data.type = curHeroData.type;
            ctrlItem.initData(go, data);
            go.onClick:Clear();
            go.onClick:Add(function()
                ctrlItem.initData(choose,data);
                 selecthero=tostring(itemVal);
                 state.selectedIndex=1;
                 if selectObj then
                    ctrlHadBattle.selectedIndex=0;                   
                end
                selectObj=go;
                ctrlHadBattle=selectObj:GetController("ctrlHadBattle");
                ctrlHadBattle.selectedIndex=1;
            end)
        end    
        if selecthero==itemVal then
            selectObj=itemObj;   
            if selectObj then
                ctrlHadBattle=selectObj:GetController("ctrlHadBattle");
                ctrlHadBattle:SetSelectedIndex(1);
            end
        end 
end

function UIDrawChooseHero.addEvent()
    this.btn_ture.onClick:Add(this.ChangeHero);
    this.backBtn.onClick:Add(this.CloseUI);
    ListenEvent(Config.EventType.Fresh_SelectDrawSave, this.updateAgain);
end

function UIDrawChooseHero.removeEvent()
    this.btn_ture.onClick:Remove(this.ChangeHero);
    this.backBtn.onClick:Remove(this.CloseUI);
    CloseEvent(Config.EventType.Fresh_SelectDrawSave, this.updateAgain);
end

function UIDrawChooseHero.ChangeHero()
    drawHandler.SendSetAppointDrawHero(selecthero)
    this.CloseUI();
end

function UIDrawChooseHero.CloseUI()
    closeUI(PanelNames.UIDrawChooseHero);
    
end

function UIDrawChooseHero.OnDestroy()
    this.removeEvent();
end

function UIDrawChooseHero.OnEnable() 
   
end

function UIDrawChooseHero.OnDisable()
    if ctrlHadBattle then
        ctrlHadBattle.selectedIndex=0;
    end
   
end


return UIDrawChooseHero;